/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.waste.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.client.waste.WasteServiceAsync;
import org.toools.gwt.wmc.jpa.Container;
import org.toools.gwt.wmc.jpa.Waste;

/**
 *
 * @author Manuel
 */
public class WasteListRenderer extends BasicListRenderer{

    BasicTogglePanel containers = new BasicTogglePanel("set containers", 1, true) {
        public void onActive() {
            clear();
            addChild(new ContainersRelation((Waste) getTag().getAttachedBusinessObject()));
        }
    };

    public WasteListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);

//        questionable if this should be done here
//        addButton(containers);
	}

    @Override
    public void setTag(TagVO tag) {
        Waste ac = (Waste) tag.getAttachedBusinessObject();
		setTitle(ac.getWasteDesc());
    }

    public class ContainersRelation extends RelationControl {
        Waste w;
        public ContainersRelation(Waste w) {
            super(false, true, true, false);
            this.w = w;
            ContainerService.Util.getInstance().getContainers(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Container a = (Container) bp;
            TagVO result = new TagVO("" + a.getId(), a.getName(), 0, 0);
            result.setAttachedBusinessObject(a);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Container>) result).toArray()));
                if((w.getContainers() != null) && (w.getContainers().size() > 0)) {
                    String[] active = new String[w.getContainers().size()];
                    int j = 0;
                    for(Iterator i = w.getContainers().iterator(); i.hasNext();) {
                        Container a = (Container) i.next();
                        active[j++] = "" + a.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Container> containers = new Vector<Container>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    containers.add((Container)tag.getAttachedBusinessObject());
                }
                w.setContainers(containers);
                ((WasteServiceAsync)getBasicControl().getService()).update(w, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

}
